from django.shortcuts import render,HttpResponse
import json
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin
from django.conf import settings
from . import models,ser
from django.core.cache import cache
from rest_framework.response import Response
# Create your views here.

#轮播图表
class BannerView(GenericViewSet, ListModelMixin):
    queryset = models.Banner.objects.filter(is_delete=False, is_show=True).order_by('orders')[:settings.BANNER_COUNTER]
    serializer_class = ser.BannerModelSerializer
    def list(self, request, *args, **kwargs):
        #把 data的数据加入缓存中
        #1 先去缓存拿数据
        banner_list = cache.get('banner_list')
        if not banner_list:
            print('走数据库了')
            response = super().list(request, *args, **kwargs)
            # print(response.data) #OrderedDict([('name', 'banner1'), ('link', 'free-course'), ('img', 'http://127.0.0.1:8001/media/banner/banner1.png')
            #加到缓存中
            cache.set('banner_list', response.data, 60*60*24)
        return Response(data=banner_list)